package view.swing;

import javax.swing.DefaultListModel;
import model.ScoreDB;
import model.ScoreDB.Score;

/**
 *
 * @author gilaquila
 */
public class HighScorePanel extends javax.swing.JPanel {

    /**
     * Creates new form HighScorePanel
     */
    public HighScorePanel() {
        listModel = new DefaultListModel();

        initComponents();
    }

    public void updateScoreList() {
        Runnable runn = new Runnable() {
            @Override
            public void run() {
                listModel.removeAllElements();
                
                Score scores[] = ScoreDB.getHighScore();
                
                listModel.ensureCapacity(scores.length);
                
                for (int i = 0; i < scores.length; i++) {
                    listModel.addElement((i + 1) + ". " + scores[i].toString());
                }
            }
        };
        
        Thread a = new Thread(runn);
        
        a.start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        scoreList = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("HighScore");

        jList1.setModel(listModel);
        jList1.setFocusable(false);
        scoreList.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(scoreList, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scoreList)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane scoreList;
    // End of variables declaration//GEN-END:variables
    private final DefaultListModel listModel;
}
